package com.gitee.deeround.javaprotect.agent.util;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CommonUtils {
    public static String getClassEncodePath(String targetDir, String className) {
        String path = "/META-INF/.protect/" + className;
        if (targetDir == null) {
            return path;
        }
        return new File(targetDir, path).getAbsolutePath();
    }

    public static String getMD5(String input) {
        try {
            // 获取一个MD5算法的MessageDigest实例
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 计算输入字符串的MD5值
            byte[] messageDigest = md.digest(input.getBytes());

            // 创建16进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte aMessageDigest : messageDigest) {
                String shaHex = Integer.toHexString(aMessageDigest & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            //e.printStackTrace();
        }
        return input;
    }
}
